﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Repositories;
using Estsh.Core.Wms.IRepositories;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Repositories
{
    public class OutSourceShippingRepository: BaseRepository<BaseEntity>,IOutSourceShippingRepository
    {
        public OutSourceShippingRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {

        }

        public List<WmsOutstock_Extend> GetOutstocks(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT * FROM dbo.wms_outstock (nolock) WHERE order_type=100 AND order_status IN (10,30) AND enabled='Y'");
                if (!string.IsNullOrEmpty(orderNo))
                {
                    sql.Append($" AND order_no LIKE '%{orderNo}%'");
                }

                return dbConn.Query<WmsOutstock_Extend>(sql.ToString()).ToList();
            }
        }
        public List<WmsOutstockDetail> GetOutstockDetails(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append($"SELECT * FROM dbo.wms_outstock_detail (nolock) WHERE order_no='{orderNo}' AND enabled='Y'");
                
                return dbConn.Query<WmsOutstockDetail>(sql.ToString()).ToList();
            }
        }


    }
}
